package com.googlecode.util;

import java.util.LinkedHashSet;
import java.util.Iterator;

/**
 * User: Howard.Lin - linhua3721@gmail.com
 * DateTime: 2009-5-12 14:32:12
 * 工具类：继承LinkeHashSet类的所有特性
 * 特有功能：保持该类的固定容量大小不变
 */
public class HashQueueSet<E> extends LinkedHashSet<E> {
    private int initialCapacity;
    public HashQueueSet(int initialCapacity){
         this.initialCapacity =  initialCapacity;
    }
    @Override
    public boolean add(E e){
        if(super.contains(e)){
            return false;
        }
        if(this.size()>=initialCapacity){
            Iterator<E> iterator = this.iterator();
            if (iterator.hasNext() ){
                E o = iterator.next();
                remove(o);
            }
            return super.add(e);
        }
        else
            return super.add(e);
    }
}
